package com.cn.gmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.cn.gmall.common.constant.RedisConst;
import com.cn.gmall.common.execption.GmallException;
import com.cn.gmall.common.result.ResultCodeEnum;
import com.cn.gmall.item.service.ItemService;
import com.cn.gmall.list.client.ListFeignClient;
import com.cn.gmall.model.product.*;
import com.cn.gmall.product.client.ProductFeignClient;
import io.lettuce.core.RedisClient;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

@Service
@SuppressWarnings("all")
public class ItemServiceImpl implements ItemService {
    @Autowired
   private ProductFeignClient productFeignClient;
    @Autowired
    private   RedissonClient redissonClient;

    @Autowired
    private ListFeignClient listFeignClient;
    @Autowired
    private  ThreadPoolExecutor threadPoolExecutor;

    /**
     * ItemService
     *同步
     * @param skuId
     * @return
     */
//    @Override
//    public Map<String, Object> getSkuById(Long skuId) {
//
//
////判断当前id 是否存在布隆过滤器
//        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(RedisConst.SKU_BLOOM_FILTER);
//        if (!bloomFilter.contains(skuId)) {
////    布隆过滤器 里面没有查询到
//            throw new GmallException("所查询的skuid不存在",ResultCodeEnum.FAIL.getCode());
//        }
//
//
//
//        HashMap<String, Object> result = new HashMap<>();
//
////    1.     skuAttrList  sku平台属性
//        SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
//
//        if (ObjectUtils.isEmpty(skuInfo)) {
//            throw new GmallException("获取不到对应详情信息", ResultCodeEnum.FAIL.getCode());
//        }
//
//        result.put("skuInfo", skuInfo);
////    2.        categoryView  BaseCategoryView
//        BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
//        if (ObjectUtils.isEmpty(categoryView)) {
//            throw new GmallException("获取不到三级分类信息", ResultCodeEnum.FAIL.getCode());
//        }
//        result.put("categoryView", categoryView);
//
////     3. spuPosterList     spu海报数据
//        List<SpuPoster> spuPosterList = productFeignClient.findSpuPosterBySpuId(skuInfo.getSpuId());
//        result.put("spuPosterList",spuPosterList);
//        result.put("spuPosterList", productFeignClient.findSpuPosterBySpuId(skuInfo.getSpuId()));
//
////      4.1      spuSaleAttrList  spu销售属性
//        List<SpuSaleAttr> spuSaleAttrList = productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
//        result.put("spuSaleAttrList",spuSaleAttrList);
//        result.put("spuSaleAttrList", productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId()));
//
//        //    4.2     spuSaleAttrValueList
////         5. price
//        BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
//        result.put("price",skuPrice);
//        result.put("price", productFeignClient.getSkuPrice(skuId));
////        6. valuesSkuJson
//
//        Map skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
//        String valuesSkuJson = JSON.toJSONString(skuValueIdsMap);
//        result.put("valuesSkuJson",valuesSkuJson);
//        result.put("valuesSkuJson", JSON.toJSONString(productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId())));
//
////        7. skuAttrList
//
//        List<BaseAttrInfo> attrList = productFeignClient.getAttrList(skuId);
//
//        List<Map<String, String>> maps = attrList.stream().map(baseAttrInfo -> {
//            HashMap<String, String> attMap = new HashMap<>();
//            attMap.put("attrName", baseAttrInfo.getAttrName());
//            attMap.put("attrValue", baseAttrInfo.getAttrValueList().get(0).getValueName());
//            return attMap;
//        }).collect(Collectors.toList());
//
////        result.put("skuAttrList",JSON.toJSONString(productFeignClient.getAttrList(skuId)));
//        result.put("skuAttrList", maps);
//
//        return result;
//
//    }


    /**
     * 异步编排
     * @param skuId
     * @return
     */
    @Override
    public Map<String, Object> getSkuById(Long skuId) {

/*
//判断当前id 是否存在布隆过滤器
        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(RedisConst.SKU_BLOOM_FILTER);
        if (!bloomFilter.contains(skuId)) {
//    布隆过滤器 里面没有查询到
            throw new GmallException("所查询的skuid不存在",ResultCodeEnum.FAIL.getCode());
        }
*/
        HashMap<String, Object> result = new HashMap<>();

        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //    1.     skuAttrList  sku平台属性
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
          /*  if (ObjectUtils.isEmpty(skuInfo)) {
                throw new GmallException("获取不到对应详情信息", ResultCodeEnum.FAIL.getCode());
            }*/
            result.put("skuInfo", skuInfo);

            return skuInfo;

        }, threadPoolExecutor);



        CompletableFuture<Void> categoryViewCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            //    2.        categoryView  BaseCategoryView
//            BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
   /*     if (ObjectUtils.isEmpty(categoryView)) {
            throw new GmallException("获取不到三级分类信息", ResultCodeEnum.FAIL.getCode());
        }*/
//            result.put("categoryView", categoryView);
            result.put("categoryView", productFeignClient.getCategoryView(skuInfo.getCategory3Id()));
        }, threadPoolExecutor);


        CompletableFuture<Void> spuPosterListCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            //     3. spuPosterList     spu海报数据
       /* List<SpuPoster> spuPosterList = productFeignClient.findSpuPosterBySpuId(skuInfo.getSpuId());
        result.put("spuPosterList",spuPosterList);*/
            result.put("spuPosterList", productFeignClient.findSpuPosterBySpuId(skuInfo.getSpuId()));

        }, threadPoolExecutor);


        CompletableFuture<Void> spuSaleAttrListCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            //      4.1      spuSaleAttrList  spu销售属性
      /*  List<SpuSaleAttr> spuSaleAttrList = productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
        result.put("spuSaleAttrList",spuSaleAttrList);*/
            result.put("spuSaleAttrList", productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId()));

        }, threadPoolExecutor);

        CompletableFuture<Void> valuesSkuJsonCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
//        5. valuesSkuJson
     /*   Map skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
        String valuesSkuJson = JSON.toJSONString(skuValueIdsMap);
        result.put("valuesSkuJson",valuesSkuJson);*/
            result.put("valuesSkuJson", JSON.toJSONString(productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId())));

        }, threadPoolExecutor);

        CompletableFuture<Void> priceCompletableFuture = CompletableFuture.runAsync(() -> {
            //         6. price
       /* BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
        result.put("price",skuPrice);*/
            result.put("price", productFeignClient.getSkuPrice(skuId));
        }, threadPoolExecutor);


        CompletableFuture<Void> skuAttrListCompletableFuture = CompletableFuture.runAsync(() -> {
            //        7. skuAttrList

//            List<BaseAttrInfo> attrList = productFeignClient.getAttrList(skuId);

//            List<Map<String, String>> maps = attrList.stream().map(baseAttrInfo -> {
//                HashMap<String, String> attMap = new HashMap<>();
//                attMap.put("attrName", baseAttrInfo.getAttrName());
//                attMap.put("attrValue", baseAttrInfo.getAttrValueList().get(0).getValueName());
//                return attMap;
//            }).collect(Collectors.toList());
//
////        result.put("skuAttrList",JSON.toJSONString(productFeignClient.getAttrList(skuId)));
//            result.put("skuAttrList", maps);
            result.put("skuAttrList", productFeignClient.getAttrList(skuId).stream().map(baseAttrInfo -> {
                HashMap<String, String> attMap = new HashMap<>();
                attMap.put("attrName", baseAttrInfo.getAttrName());
                attMap.put("attrValue", baseAttrInfo.getAttrValueList().get(0).getValueName());
                return attMap;
            }).collect(Collectors.toList()));

        }, threadPoolExecutor);
//        统计点击量 /热点统计

        CompletableFuture<Void> hotScoreCompletableFuture = CompletableFuture.runAsync(() -> {

            listFeignClient.incrHotScore(skuId);
        }, threadPoolExecutor);


//线程的等待
        CompletableFuture.allOf(skuInfoCompletableFuture,
                categoryViewCompletableFuture,
                spuPosterListCompletableFuture
              ,spuSaleAttrListCompletableFuture,
                valuesSkuJsonCompletableFuture,
                priceCompletableFuture,
                skuAttrListCompletableFuture,
                hotScoreCompletableFuture).join();



        return result;

    }
}
